<?php
declare (strict_types = 1);

namespace app\middleware;

class CheckAuth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        if (in_array($request->url(), config('app.no_login'))) {
            return $next($request);
        }
        
        $authorization = $request->header('Authorization');
        if (!$authorization) {
            return error('token not found');
        }

        list(, $token) = explode(" ", $authorization);
        try {
            if (checkToken($token) ) {
                return $next($request);
            }
        } catch (\Throwable $th) {
            return error($th->getMessage());
        }

    }
}
